package com.pcbly.web.components.utils.theme;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;

import com.pcbly.web.components.utils.property.OfficeGlobals;


@SuppressWarnings("unchecked")
public class ThemeType implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2071883436445944059L;
	public static final ThemeType GLOBAL = new ThemeType("Global", 0);
	public static final ThemeType URL;
	public static final ThemeType COMMUNITY;
	protected static ThemeType types[];
	private final String name;
	private int precedence;

	static {
		URL = new ThemeType("Url", OfficeGlobals.getOfficeIntProperty(
				"theme.map.type.Url.priority", 1));
		COMMUNITY = new ThemeType("Community", OfficeGlobals
				.getOfficeIntProperty("theme.map.type.Community.priority", 2));
		types = (new ThemeType[] { URL, COMMUNITY });
	}

	// public static class TypePrecedenceTask extends AbstractClusterTask
	// {
	//
	// public void execute()
	// {
	// ThemeType.fromString(name).precedence = precedence;
	// }
	//
	// public Object getResult()
	// {
	// return null;
	// }
	//
	// private String name;
	// private int precedence;
	//
	// public TypePrecedenceTask()
	// {
	// }
	//
	// public TypePrecedenceTask(String name, int precedence)
	// {
	// this.name = name;
	// this.precedence = precedence;
	// }
	// }
	//

	public ThemeType(String name, int precedence) {
		this.name = name;
		this.precedence = precedence;
	}

	public void setPrecedence(int precedence) {
		this.precedence = precedence;
		String s = String.valueOf(precedence);
		String s1 = (new StringBuilder()).append("theme.map.type.")
				.append(name).append(".priority").toString();
		OfficeGlobals.setOfficeProperty(s1, s);
		// CacheFactory.doClusterTask(new TypePrecedenceTask(name, precedence));
	}

	public int getPrecedence() {
		return precedence;
	}

	public String toString() {
		return name;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		ThemeType themeType = (ThemeType) o;
		return name == null ? themeType.name == null : name
				.equals(themeType.name);
	}

	public int hashCode() {
		return name == null ? 0 : name.hashCode();
	}

	public static ThemeType fromString(String name) {
		if (GLOBAL.toString().equals(name))
			return GLOBAL;
		ThemeType arr$[] = types;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			ThemeType themeType = arr$[i$];
			if (themeType.toString().equals(name))
				return themeType;
		}

		return null;
	}

	public static ThemeType[] getTypes() {
		Arrays.sort(types, new Comparator() {

			public int compare(ThemeType type1, ThemeType type2) {
				return type1.precedence - type2.precedence;
			}

			public int compare(Object x0, Object x1) {
				return compare((ThemeType) x0, (ThemeType) x1);
			}

		});
		ThemeType copy[] = new ThemeType[types.length];
		System.arraycopy(types, 0, copy, 0, copy.length);
		return copy;
	}

}
